This tutorial illustrates the core visualization utilities available in Ax.
import numpy as np
from ax.service.ax_client import AxClient
from ax.modelbridge.cross_validation import cross_validate
from ax.plot.contour import interact_contour
from ax.plot.diagnostic import interact_cross_validation
from ax.plot.scatter import(
interact_fitted,
plot_objective_vs_constraints,
tile_fitted,
)
from ax.plot.slice import plot_slice
from ax.utils.measurement.synthetic_functions import hartmann6
from ax.utils.notebook.plotting import render, init_notebook_plotting
init_notebook_plotting()
[INFO 09-16 05:46:54] ax.utils.notebook.plotting: Injecting Plotly library into cell. Do not overwrite or delete cell.
The vizualizations require an experiment object and a model fit on the evaluated data. The routine below is a copy of the Service API tutorial, so the explanation here is omitted. Retrieving the experiment and model objects for each API paradigm is shown in the respective tutorials
noise_sd = 0.1
param_names = [f"x{i+1}" for i in range(6)] # x1, x2, ..., x6
def noisy_hartmann_evaluation_function(parameterization):
x = np.array([parameterization.get(p_name) for p_name in param_names])
noise1, noise2 = np.random.normal(0, noise_sd, 2)
return {
"hartmann6": (hartmann6(x) + noise1, noise_sd),
"l2norm": (np.sqrt((x ** 2).sum()) + noise2, noise_sd)
}
ax_client = AxClient()
ax_client.create_experiment(
name="test_visualizations",
parameters=[
{
"name": p_name,
"type": "range",
"bounds": [0.0, 1.0],
}
for p_name in param_names
],
objective_name="hartmann6",
minimize=True,
outcome_constraints=["l2norm <= 1.25"]
)
[INFO 09-16 05:46:54] ax.service.ax_client: Starting optimization with verbose logging. To disable logging, set the `verbose_logging` argument to `False`. Note that float values in the logs are rounded to 6 decimal points.
[INFO 09-16 05:46:54] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x1. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 09-16 05:46:54] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x2. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 09-16 05:46:54] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x3. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 09-16 05:46:54] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x4. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 09-16 05:46:54] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x5. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 09-16 05:46:54] ax.service.utils.instantiation: Inferred value type of ParameterType.FLOAT for parameter x6. If that is not the expected value type, you can explicity specify 'value_type' ('int', 'float', 'bool' or 'str') in parameter dict.
[INFO 09-16 05:46:54] ax.service.utils.instantiation: Created search space: SearchSpace(parameters=[RangeParameter(name='x1', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x2', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x3', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x4', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x5', parameter_type=FLOAT, range=[0.0, 1.0]), RangeParameter(name='x6', parameter_type=FLOAT, range=[0.0, 1.0])], parameter_constraints=[]).
[INFO 09-16 05:46:54] ax.modelbridge.dispatch_utils: Using Bayesian optimization since there are more ordered parameters than there are categories for the unordered categorical parameters.
[INFO 09-16 05:46:54] ax.modelbridge.dispatch_utils: Using Bayesian Optimization generation strategy: GenerationStrategy(name='Sobol+GPEI', steps=[Sobol for 12 trials, GPEI for subsequent trials]). Iterations after 12 will take longer to generate due to model-fitting.
for i in range(20):
parameters, trial_index = ax_client.get_next_trial()
# Local evaluation here can be replaced with deployment to external system.
ax_client.complete_trial(trial_index=trial_index, raw_data=noisy_hartmann_evaluation_function(parameters))
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 0 with parameters {'x1': 0.625047, 'x2': 0.56678, 'x3': 0.204441, 'x4': 0.33372, 'x5': 0.36633, 'x6': 0.207956}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 0 with data: {'hartmann6': (-0.364049, 0.1), 'l2norm': (0.891059, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 1 with parameters {'x1': 0.232647, 'x2': 0.649302, 'x3': 0.633998, 'x4': 0.844496, 'x5': 0.93883, 'x6': 0.649115}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 1 with data: {'hartmann6': (-0.091447, 0.1), 'l2norm': (1.777703, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 2 with parameters {'x1': 0.311215, 'x2': 0.353236, 'x3': 0.058498, 'x4': 0.674277, 'x5': 0.279608, 'x6': 0.616229}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 2 with data: {'hartmann6': (-0.425795, 0.1), 'l2norm': (1.165736, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 3 with parameters {'x1': 0.176085, 'x2': 0.13451, 'x3': 0.909378, 'x4': 0.540793, 'x5': 0.132394, 'x6': 0.333082}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 3 with data: {'hartmann6': (-0.383193, 0.1), 'l2norm': (1.211684, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 4 with parameters {'x1': 0.243269, 'x2': 0.081962, 'x3': 0.601607, 'x4': 0.469362, 'x5': 0.666965, 'x6': 0.106685}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 4 with data: {'hartmann6': (-0.07422, 0.1), 'l2norm': (1.08518, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 5 with parameters {'x1': 0.386201, 'x2': 0.518646, 'x3': 0.623288, 'x4': 0.090945, 'x5': 0.882507, 'x6': 0.207993}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 5 with data: {'hartmann6': (-0.212637, 0.1), 'l2norm': (1.253983, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 6 with parameters {'x1': 0.285164, 'x2': 0.757439, 'x3': 0.854665, 'x4': 0.124244, 'x5': 0.32774, 'x6': 0.104944}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 6 with data: {'hartmann6': (-0.286559, 0.1), 'l2norm': (1.141076, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 7 with parameters {'x1': 0.987928, 'x2': 0.318252, 'x3': 0.277917, 'x4': 0.925709, 'x5': 0.591466, 'x6': 0.586302}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 7 with data: {'hartmann6': (0.013999, 0.1), 'l2norm': (1.734199, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 8 with parameters {'x1': 0.277762, 'x2': 0.021904, 'x3': 0.789102, 'x4': 0.125379, 'x5': 0.125253, 'x6': 0.812441}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 8 with data: {'hartmann6': (-1.21261, 0.1), 'l2norm': (1.137067, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 9 with parameters {'x1': 0.303625, 'x2': 0.303927, 'x3': 0.575075, 'x4': 0.625798, 'x5': 0.422538, 'x6': 0.582806}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 9 with data: {'hartmann6': (-0.738061, 0.1), 'l2norm': (1.036592, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 10 with parameters {'x1': 0.882285, 'x2': 0.408409, 'x3': 0.893356, 'x4': 0.314959, 'x5': 0.549738, 'x6': 0.602664}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 10 with data: {'hartmann6': (-0.07384, 0.1), 'l2norm': (1.671985, 0.1)}.
[INFO 09-16 05:46:54] ax.service.ax_client: Generated new trial 11 with parameters {'x1': 0.923067, 'x2': 0.598978, 'x3': 0.656342, 'x4': 0.481204, 'x5': 0.17497, 'x6': 0.3094}.
[INFO 09-16 05:46:54] ax.service.ax_client: Completed trial 11 with data: {'hartmann6': (-0.096332, 0.1), 'l2norm': (1.484422, 0.1)}.
[INFO 09-16 05:47:08] ax.service.ax_client: Generated new trial 12 with parameters {'x1': 0.275702, 'x2': 0.07362, 'x3': 0.729887, 'x4': 0.208892, 'x5': 0.174746, 'x6': 0.786196}.
[INFO 09-16 05:47:08] ax.service.ax_client: Completed trial 12 with data: {'hartmann6': (-1.893518, 0.1), 'l2norm': (1.006559, 0.1)}.
[INFO 09-16 05:47:14] ax.service.ax_client: Generated new trial 13 with parameters {'x1': 0.26922, 'x2': 0.104804, 'x3': 0.687236, 'x4': 0.263512, 'x5': 0.203582, 'x6': 0.778663}.
[INFO 09-16 05:47:14] ax.service.ax_client: Completed trial 13 with data: {'hartmann6': (-2.256452, 0.1), 'l2norm': (1.094163, 0.1)}.
[INFO 09-16 05:47:20] ax.service.ax_client: Generated new trial 14 with parameters {'x1': 0.257261, 'x2': 0.132322, 'x3': 0.63386, 'x4': 0.311222, 'x5': 0.226857, 'x6': 0.788629}.
[INFO 09-16 05:47:20] ax.service.ax_client: Completed trial 14 with data: {'hartmann6': (-2.476546, 0.1), 'l2norm': (1.104279, 0.1)}.
[INFO 09-16 05:47:33] ax.service.ax_client: Generated new trial 15 with parameters {'x1': 0.256904, 'x2': 0.085657, 'x3': 0.627165, 'x4': 0.364063, 'x5': 0.248097, 'x6': 0.831391}.
[INFO 09-16 05:47:33] ax.service.ax_client: Completed trial 15 with data: {'hartmann6': (-2.295317, 0.1), 'l2norm': (1.04317, 0.1)}.
[INFO 09-16 05:47:38] ax.service.ax_client: Generated new trial 16 with parameters {'x1': 0.234491, 'x2': 0.185629, 'x3': 0.589184, 'x4': 0.285237, 'x5': 0.250177, 'x6': 0.816241}.
[INFO 09-16 05:47:38] ax.service.ax_client: Completed trial 16 with data: {'hartmann6': (-2.426764, 0.1), 'l2norm': (1.028576, 0.1)}.
[INFO 09-16 05:47:45] ax.service.ax_client: Generated new trial 17 with parameters {'x1': 0.247478, 'x2': 0.160364, 'x3': 0.565936, 'x4': 0.322961, 'x5': 0.166311, 'x6': 0.783972}.
[INFO 09-16 05:47:45] ax.service.ax_client: Completed trial 17 with data: {'hartmann6': (-2.048737, 0.1), 'l2norm': (1.035636, 0.1)}.
[INFO 09-16 05:47:47] ax.service.ax_client: Generated new trial 18 with parameters {'x1': 0.237167, 'x2': 0.130069, 'x3': 0.629209, 'x4': 0.279715, 'x5': 0.280809, 'x6': 0.762609}.
[INFO 09-16 05:47:47] ax.service.ax_client: Completed trial 18 with data: {'hartmann6': (-2.881078, 0.1), 'l2norm': (1.012408, 0.1)}.
[INFO 09-16 05:47:51] ax.service.ax_client: Generated new trial 19 with parameters {'x1': 0.205866, 'x2': 0.10404, 'x3': 0.613631, 'x4': 0.266555, 'x5': 0.323963, 'x6': 0.738591}.
[INFO 09-16 05:47:51] ax.service.ax_client: Completed trial 19 with data: {'hartmann6': (-2.937414, 0.1), 'l2norm': (1.010927, 0.1)}.
The plot below shows the response surface for hartmann6 metric as a function of the x1, x2 parameters.
The other parameters are fixed in the middle of their respective ranges, which in this example is 0.5 for all of them.
# this could alternately be done with `ax.plot.contour.plot_contour`
render(ax_client.get_contour_plot(param_x="x1", param_y="x2", metric_name='hartmann6'))
[INFO 09-16 05:47:51] ax.service.ax_client: Retrieving contour plot with parameter 'x1' on X-axis and 'x2' on Y-axis, for metric 'hartmann6'. Remaining parameters are affixed to the middle of their range.
The plot below allows toggling between different pairs of parameters to view the contours.
model = ax_client.generation_strategy.model
render(interact_contour(model=model, metric_name='hartmann6'))
This plot illustrates the tradeoffs achievable for 2 different metrics. The plot takes the x-axis metric as input (usually the objective) and allows toggling among all other metrics for the y-axis.
This is useful to get a sense of the pareto frontier (i.e. what is the best objective value achievable for different bounds on the constraint)
render(plot_objective_vs_constraints(model, 'hartmann6', rel=False))
CV plots are useful to check how well the model predictions calibrate against the actual measurements. If all points are close to the dashed line, then the model is a good predictor of the real data.
cv_results = cross_validate(model)
render(interact_cross_validation(cv_results))
Slice plots show the metric outcome as a function of one parameter while fixing the others. They serve a similar function as contour plots.
render(plot_slice(model, "x2", "hartmann6"))
Tile plots are useful for viewing the effect of each arm.
render(interact_fitted(model, rel=False))
Total runtime of script: 1 minutes, 19.05 seconds.